﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ST.Common.Data;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Buz.Base;
using ST.Buz.Base.Entity;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ST.Buz.Service
{
	public partial interface IServiceSysUserRole
	{
		/// <summary>
		/// 获取用户的所有权限ID
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		List<int> GetRolesList(int userID);

		/// <summary>
		/// 获取用户的所有权限ID
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		int[] GetRolesArray(int userID);

		/// <summary>
		/// 获取用户的所有权限代码
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		string[] GetRoleCodesArray(int userID);

		/// <summary>
		/// 获取用户的所有权限名称
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		string[] GetRoleNamesArray(int userID);

		/// <summary>
		/// 获取指定角色所拥有的模块、菜单、功能
		/// </summary>
		/// <param name="roles"></param>
		/// <returns></returns>
		Sys_Module[] GetModulesAndServicesByRole(int[] roles);

		/// <summary>
		/// 获取指定用户所拥有的模块、菜单、功能
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		Sys_Module[] GetModulesAndServicesByUserID(int userID);

		void DeleteByUserID(int userID);
	}

	public partial class ServiceSysUserRole
	{
		public ServiceSysUserRole(BuzContext ctx,
			ILogger<ServiceSysUserRole> log)
		{
			context = ctx;
			logger = log;
		}

		/// <summary>
		/// 获取用户的所有权限ID
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		public List<int> GetRolesList(int userID)
		{
			var userRoleQuery = from ur in context.SysUserRole
								where ur.UserID == userID
								select ur.RoleID;
			return userRoleQuery.ToList();
		}

		/// <summary>
		/// 获取用户的所有权限ID
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		public int[] GetRolesArray(int userID)
		{
			var userRoleQuery = from ur in context.SysUserRole
								where ur.UserID == userID
								select ur.RoleID;
			return userRoleQuery.ToArray();
		}

		/// <summary>
		/// 获取用户的所有权限代码
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		public string[] GetRoleCodesArray(int userID)
		{
			var userRoleQuery = from ur in context.SysUserRole
								join role in context.SysRole on ur.RoleID equals role.ID
								where ur.UserID == userID
								select role.Code;
			return userRoleQuery.ToArray();
		}

		/// <summary>
		/// 获取用户的所有权限名称
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		public string[] GetRoleNamesArray(int userID)
		{
			var userRoleQuery = from ur in context.SysUserRole
								join role in context.SysRole on ur.RoleID equals role.ID
								where ur.UserID == userID
								select role.Name;
			return userRoleQuery.ToArray();
		}

		/// <summary>
		/// 获取指定角色所拥有的模块、菜单、功能
		/// </summary>
		/// <param name="roles"></param>
		/// <returns></returns>
		public Sys_Module[] GetModulesAndServicesByRole(int[] roles)
		{
			if (roles == null || roles.Length == 0)
			{
				return null;
			}
			var roleActionQuery = from ra in context.SysRoleAction
								  where roles.Contains(ra.RoleID)
								  select ra.ActionID;
			int[] actions = roleActionQuery.ToArray();

			var serviceQuery = from ma in context.SysModuleAction
							   where actions.Contains(ma.ID) && ma.Visible
							   select ma.ServiceID;
			int[] services = serviceQuery.ToArray();

			var msQuery = from m in context.SysModule
						  join ms in context.SysModuleService on m.ID equals ms.ModuleID
						  where services.Contains(ms.ID) && ms.Visible && m.Visible
						  orderby m.Sort, m.ID, ms.Sort, ms.ID
						  select new
						  {
							  ModuleID = m.ID,
							  ModuleCode = m.Code,
							  ModuleName = m.Name,
							  ModuleIcon = m.Icon,
							  ServiceID = ms.ID,
							  ServiceCode = ms.Code,
							  ServiceName = ms.Name,
							  ServiceUrl = ms.Url
						  };
			Sys_Module moduleLast = null;
			List<Sys_Module> lstModule = new List<Sys_Module>();
			foreach (var ms in msQuery)
			{
				if (moduleLast == null || moduleLast.ID != ms.ModuleID)
				{
					moduleLast = new Sys_Module()
					{
						ID = ms.ModuleID,
						Code = ms.ModuleCode,
						Name = ms.ModuleName,
						Icon = ms.ModuleIcon
					};
					moduleLast.Services = new List<Sys_ModuleService>();
					lstModule.Add(moduleLast);
				}
				moduleLast.Services.Add(new Sys_ModuleService()
				{
					ID = ms.ServiceID,
					Code = ms.ServiceCode,
					Name = ms.ServiceName,
					Url = ms.ServiceUrl
				});
			}
			return lstModule.ToArray();
		}

		/// <summary>
		/// 获取指定用户所拥有的模块、菜单、功能
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		public Sys_Module[] GetModulesAndServicesByUserID(int userID)
		{
			var userRoleQuery = from ur in context.SysUserRole
								where ur.UserID == userID
								select ur.RoleID;
			int[] roles = userRoleQuery.ToArray();
			if (roles != null && roles.Length > 0)
			{
				return GetModulesAndServicesByRole(roles);
			}
			else
			{
				return null;
			}
		}

		public void DeleteByUserID(int userID)
		{
			context.Database.ExecuteSqlRaw("DELETE FROM Sys_UserRole WHERE UserID={0}", userID);
		}
	}
}